package cc.smarnet.caihou.phone.warehouse;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cc.smarnet.caihou.IPConfig;

import cc.smarnet.caihou.LoginActivity;
import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.Warehouse;
import cc.smarnet.caihou.phone.bracts.BractsDetailActivity;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.RequestBody;
/**
 * 入库信息详情的Activity
 * */
public class WarehouseDetailActivity extends AppCompatActivity {
    private ImageButton imageButton;
    private Spinner baseNameSpinner;//来源基地
    private Spinner greenhouseNameSpinner;//来源大棚
    private Spinner varietySpinner;//采收品种
    private Spinner packingSpecificationsSpinner;//打包规格
    private Spinner flowerPoleLengthSpinner;//花杆长度
    private Spinner classesSpinner;//花杆长度
    private TextView varietyCodeTextView;//品种编码;
    private EditText tagNumEditText;//入库数量;
    private EditText sortTimeEditText;//分拣日期;
    private TextView createUserTextView;//创建人;
    private TextView createTimeTextView;//创建日期;
    private ProgressBar progressBar;
    private Button editButton;
    private Button saveButton;
    private Button deleteButton;

    Warehouse warehouse;

    private String baseNameId;//来源基地id
    private String greenhouseNameId;//来源大棚id
    private String variety;//采收品种
    private String varietyCode;//品种编码
    private String packingSpecifications;//打包规格
    private String flowerPoleLength;//花杆长度
    private String classes;//花卉等级
    private int tagNum;//入库数量
    private String sortTime;//分拣日期
    private String createUser;//创建人
    private String createTime;//创建日期

    private String token;

    int baseNamePosition = 0;//默认基地在接口返回的基地列表的位置
    int greenhouseNamePosition = 0;//默认大棚在接口返回的大棚列表的位置
    int varietyPosition = 0;//默认采收品种在接口返回的采收品种列表的位置
    int packingSpecificationsPosition = 0;//默认打包规格在接口返回的打包规格列表的位置
    int flowerPoleLengthPosition = 0;//默认花杆长度在接口返回的花杆长度列表的位置
    int classesPosition = 0;//默认花卉等级在接口返回的花卉等级列表的位置

    List<String> baseNameList = new ArrayList<>();
    List<String> greenhouseNameList = new ArrayList<>();
    List<String> varietyList = new ArrayList<>();
    List<String> varietyCodeList = new ArrayList<>();
    List<String> packingSpecificationsList = new ArrayList<>();
    List<String> flowerPoleLengthList = new ArrayList<>();
    List<String> classesList = new ArrayList<>();

    List<String> baseNameIdList = new ArrayList<>();
    List<String> greenhouseNameIdList = new ArrayList<>();

    int mYear, mMonth, mDay;

    private final String basePath = "/sys/dict/getDictItems";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_warehouse_detail);

        getSupportActionBar().hide();//隐藏标题栏

        initViews();
        //获取从warehouseActivity中传过来的值
        warehouse = (Warehouse) getIntent().getSerializableExtra("warehouseDetail");

        varietyCode=warehouse.getVarietyCode();
        tagNum=warehouse.getTagNum();
        sortTime=warehouse.getSortTime();
        createUser=warehouse.getCreateUser();
        createTime=warehouse.getCreateTime();

        varietyCodeTextView.setText(varietyCode);
        tagNumEditText.setText(String.valueOf(tagNum));
        sortTimeEditText.setText(sortTime);
        createUserTextView.setText(createUser);
        createTimeTextView.setText(createTime);
        setDisEnabled();
        getToken();
        getBaseName();//获取来源基地
        getVariety();//获取采收品种
        getPackingSpecifications();//获取打包规格
        getFlowerPoleLength();//获取花杆长度
        getClasses();//获取花卉等级


        getDate();// 获取当前的日期
        // 点击分拣日期的监听
        sortTimeEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(WarehouseDetailActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int day) {
                                mYear = year;
                                mMonth = month;
                                mDay = day;
                                String transMonth = String.format("%02d", mMonth + 1);// 如果月份小于10的话进行补0
                                String transDay = String.format("%02d", mDay);// 如果日小于10的话进行补0
                                sortTime = mYear + "-" + transMonth + "-" + transDay;
                                sortTimeEditText.setText(sortTime);
                            }
                        }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        //点击编辑按钮的监听
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editButton.setVisibility(View.GONE);
                saveButton.setVisibility(View.VISIBLE);
                setEnabled();
            }
        });

        //点击保存按钮的监听
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取采收支数的值
                if (tagNumEditText.getText().toString().equals("")) {
                    AlertDialog alertDialog = new AlertDialog.Builder(WarehouseDetailActivity.this).create();
                    alertDialog.setMessage("请输入入库数量！");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    alertDialog.show();
                    return;
                } else {
                    tagNum = Integer.parseInt(tagNumEditText.getText().toString());
                }

                if (sortTimeEditText.getText().toString().equals("点击选择日期")) {
                    AlertDialog alertDialog = new AlertDialog.Builder(WarehouseDetailActivity.this).create();
                    alertDialog.setMessage("请选择分拣日期！");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    alertDialog.show();
                    return;
                } else {
                    // 获取分拣日期
                    sortTime = sortTimeEditText.getText().toString();
                }

                // 保存入库信息
                save();
            }
        });

        //点击删除按钮的监听
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(WarehouseDetailActivity.this).create();
                alertDialog.setMessage("是否确认删除");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        delete();
                    }
                });
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
        });

        // 点击返回按钮的监听
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initViews() {
        imageButton = findViewById(R.id.warehouse_detail_return);
        baseNameSpinner = findViewById(R.id.warehouse_detail_baseName_spinner);
        greenhouseNameSpinner = findViewById(R.id.warehouse_detail_greenhouseName_spinner);
        varietySpinner = findViewById(R.id.warehouse_detail_variety_spinner);
        packingSpecificationsSpinner = findViewById(R.id.warehouse_detail_packingSpecifications_spinner);
        flowerPoleLengthSpinner = findViewById(R.id.warehouse_detail_flowerPoleLength_spinner);
        classesSpinner = findViewById(R.id.warehouse_detail_classes_spinner);
        varietyCodeTextView = findViewById(R.id.warehouse_detail_varietyCode_editText);
        tagNumEditText = findViewById(R.id.warehouse_detail_tagNum_editText);
        sortTimeEditText = findViewById(R.id.warehouse_detail_sortTime_editText);
        createUserTextView = findViewById(R.id.warehouse_detail_createUser_textView);
        createTimeTextView = findViewById(R.id.warehouse_detail_createTime_textView);
        progressBar = findViewById(R.id.warehouse_detail_progressBar);
        editButton = findViewById(R.id.warehouse_detail_edit_button);
        saveButton = findViewById(R.id.warehouse_detail_save_button);
        deleteButton = findViewById(R.id.warehouse_detail_delete_button);
    }

    // 设置各控件不可用
    public void setDisEnabled() {
        baseNameSpinner.setEnabled(false);
        greenhouseNameSpinner.setEnabled(false);
        varietySpinner.setEnabled(false);
        packingSpecificationsSpinner.setEnabled(false);
        flowerPoleLengthSpinner.setEnabled(false);
        classesSpinner.setEnabled(false);
        tagNumEditText.setEnabled(false);
        sortTimeEditText.setEnabled(false);
        createUserTextView.setEnabled(false);
    }

    // 设置各控件可用
    public void setEnabled() {
        baseNameSpinner.setEnabled(true);
        greenhouseNameSpinner.setEnabled(true);
        varietySpinner.setEnabled(true);
        packingSpecificationsSpinner.setEnabled(true);
        flowerPoleLengthSpinner.setEnabled(true);
        classesSpinner.setEnabled(true);
        tagNumEditText.setEnabled(true);
        sortTimeEditText.setEnabled(true);
        createUserTextView.setEnabled(true);
    }

    public void getToken() {
        SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        token = sp.getString("token", null);
    }

    // 获取基地
    public void getBaseName() {
        progressBar.setVisibility(View.VISIBLE);
        String url = IPConfig.baseUrl + "/flower/plantingBase/getAllBase";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            JSONArray jsonArray = JSON.parseArray(result);

                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                String name = jsonObj.getString("name");
                                baseNameList.add(name);
                                baseNameIdList.add(jsonObj.getString("id"));

                                // 获取基地对应的基地id
                                if (warehouse.getBaseName().equals(name)) {
                                    baseNameId = jsonObj.getString("id");
                                    baseNamePosition = i;
                                }
                            }

                            if (baseNameList.size() > 0) {// 如果baseNameList有值的话为baseNameSpinner赋值，然后获取相应基地下大棚的数据
                                baseNameSpinner();
                                getGreenhouseName(baseNameId);
                            }
                        }
                    }
                });
    }

    // 获取大棚
    public void getGreenhouseName(String baseNameId) {
        String url = IPConfig.baseUrl + "/flower/flowerPlantingGreenhouse/queryByParentId/" + baseNameId;
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);
                        /*Toast.makeText(warehouseAddActivity.this, "Token失效，请重新登录", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(warehouseAddActivity.this, LoginActivity.class);
                        startActivity(intent);*/
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        progressBar.setVisibility(View.GONE);
                        greenhouseNameList.clear();
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            JSONArray jsonArray = JSON.parseArray(result);

                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                String name = jsonObj.getString("name");
                                greenhouseNameList.add(name);
                                greenhouseNameIdList.add(jsonObj.getString("id"));

                                // 获取大棚对应的大棚id
                                if (warehouse.getGreenhouseName().equals(name)) {
                                    greenhouseNameId = jsonObj.getString("id");
                                    greenhouseNamePosition = i;
                                }
                            }
                            if (greenhouseNameList.size() > 0) {// 如果greenhouseNameList有值的话为greenhouseNameList赋值
                                greenhouseNameSpinner();
                            }
                        }
                    }
                });
    }

    // 获取采收品种
    public void getVariety() {
        String url = IPConfig.baseUrl + "/flower/flowerVariety/list";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            JSONArray jsonArray = JSON.parseArray(result);

                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                String name = jsonObj.getString("name");
                                varietyList.add(name);

                                // 获取品种编码
                                String code=jsonObj.getString("code");
                                varietyCodeList.add(code);

                                if (warehouse.getVariety().equals(name)){
                                    varietyPosition=i;
                                }
                            }

                            if (varietyList.size() > 0) {
                                varietySpinner();
                            }
                        }
                    }
                });
    }

    // 获取打包规格
    public void getPackingSpecifications() {
        String url = IPConfig.baseUrl + basePath + "/planting_packing_specifications";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            JSONArray jsonArray = JSON.parseArray(result);

                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                String name = jsonObj.getString("text");
                                packingSpecificationsList.add(name);

                                if (warehouse.getPackingSpecifications().equals(name)){
                                    packingSpecificationsPosition=i;
                                }
                            }

                            if (packingSpecificationsList.size() > 0) {
                                packingSpecificationsSpinner();
                            }
                        }
                    }
                });
    }

    // 获取花杆长度
    public void getFlowerPoleLength() {
        String url = IPConfig.baseUrl + basePath + "/planting_flower_pole";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            JSONArray jsonArray = JSON.parseArray(result);

                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                String name = jsonObj.getString("text");
                                flowerPoleLengthList.add(name);

                                if (warehouse.getFlowerPoleLength().equals(name)){
                                    flowerPoleLengthPosition=i;
                                }
                            }

                            if (flowerPoleLengthList.size() > 0) {
                                flowerPoleLengthSpinner();
                            }
                        }
                    }
                });
    }

    // 获取花卉等级
    public void getClasses() {
        String url = IPConfig.baseUrl + basePath +  "/planting_flower_level";
        OkHttpUtils
                .get()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            String result = JSON.parseObject(response).getString("result");
                            JSONArray jsonArray = JSON.parseArray(result);

                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObj = jsonArray.getJSONObject(i);
                                String name = jsonObj.getString("text");
                                classesList.add(name);

                                if (warehouse.getClasses().equals(name)){
                                    classesPosition=i;
                                }
                            }

                            if (classesList.size() > 0) {
                                classesSpinner();
                            }
                        }
                    }
                });
    }

    //来源基地的spinner
    public void baseNameSpinner() {
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.my_spinner_item, baseNameList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        baseNameSpinner.setAdapter(adapter);
        //设置默认的基地值（既在毛产品信息页面时的基地，这里baseNamePosition指在返回的基地列表中的位置）
        baseNameSpinner.setSelection(baseNamePosition);
        baseNameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                baseNameId = baseNameIdList.get(i);
                getGreenhouseName(baseNameId);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    //来源大棚的spinner
    public void greenhouseNameSpinner() {
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.my_spinner_item, greenhouseNameList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        greenhouseNameSpinner.setAdapter(adapter);
        //设置默认的大棚值（既在毛产品信息页面时的大棚，这里baseNamePosition指在返回的大棚列表中的位置）
        greenhouseNameSpinner.setSelection(greenhouseNamePosition);
        greenhouseNameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                greenhouseNameId = greenhouseNameIdList.get(i);
                greenhouseNamePosition=0;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    //采收品种的spinner
    public void varietySpinner() {
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.my_spinner_item, varietyList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        varietySpinner.setAdapter(adapter);
        //设置默认的采收品种
        varietySpinner.setSelection(varietyPosition);
        varietySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                variety=varietyList.get(i);

                varietyCode=varietyCodeList.get(i);
                varietyCodeTextView.setText(varietyCode);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    //打包规格的spinner
    public void packingSpecificationsSpinner() {
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.my_spinner_item, packingSpecificationsList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        packingSpecificationsSpinner.setAdapter(adapter);
        //设置默认的打包规格
        packingSpecificationsSpinner.setSelection(packingSpecificationsPosition);
        packingSpecificationsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                packingSpecifications=packingSpecificationsList.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    //花杆长度的spinner
    public void flowerPoleLengthSpinner() {
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.my_spinner_item, flowerPoleLengthList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        flowerPoleLengthSpinner.setAdapter(adapter);
        //设置默认的花杆长度
        flowerPoleLengthSpinner.setSelection(flowerPoleLengthPosition);
        flowerPoleLengthSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                flowerPoleLength=flowerPoleLengthList.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    //花卉等级的spinner
    public void classesSpinner() {
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.my_spinner_item, classesList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        classesSpinner.setAdapter(adapter);
        //设置默认的花卉等级
        classesSpinner.setSelection(classesPosition);
        classesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                classes=classesList.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    // 获取当前日期
    public void getDate() {
        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
    }

    // 保存编辑的入库信息
    public void save() {
        progressBar.setVisibility(View.VISIBLE);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id",warehouse.getId());
        map.put("baseName",baseNameId);//基地id
        map.put("greenhouseName",greenhouseNameId);//大棚id
        map.put("variety",variety);//采收品种
        map.put("packingSpecifications",packingSpecifications);//打包规格
        map.put("flowerPoleLength",flowerPoleLength);//花杆长度
        map.put("classes",classes);//花卉等级
        map.put("varietyCode",varietyCode);//品种编码
        map.put("tagNum",tagNum);//入库数量
        map.put("sortTime",sortTime);//分拣日期
        String requestJsonString = JSON.toJSONString(map);

        String url = IPConfig.baseUrl + "/flower/flowerTagIn/edit";
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, requestJsonString);
        OkHttpUtils
                .put()
                .addHeader("X-Access-Token", token)
                .url(url)
                .requestBody(body)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        progressBar.setVisibility(View.GONE);
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            AlertDialog alertDialog = new AlertDialog.Builder(WarehouseDetailActivity.this).create();
                            alertDialog.setMessage("入库信息修改成功");
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(WarehouseDetailActivity.this, WarehouseActivity.class);
                                    startActivity(intent);
                                }
                            });
                            alertDialog.setCanceledOnTouchOutside(false);
                            alertDialog.show();
                        }
                    }
                });
    }

    // 删除毛产品信息
    public void delete() {
        progressBar.setVisibility(View.VISIBLE);
        String url = IPConfig.baseUrl + "/flower/flowerTagIn/delete/?id="+warehouse.getId();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpUtils
                .delete()
                .addHeader("X-Access-Token", token)
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(WarehouseDetailActivity.this, "Token失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(WarehouseDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        progressBar.setVisibility(View.GONE);
                        boolean success = Boolean.parseBoolean(JSON.parseObject(response).getString("success"));
                        if (success) {
                            AlertDialog alertDialog = new AlertDialog.Builder(WarehouseDetailActivity.this).create();
                            alertDialog.setMessage("入库信息删除成功");
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(WarehouseDetailActivity.this, WarehouseActivity.class);
                                    startActivity(intent);
                                }
                            });
                            alertDialog.setCanceledOnTouchOutside(false);
                            alertDialog.show();
                        }
                    }
                });
    }
}